<?php

require('config.php');
require('constant.php');
require('common.php');

Class auc_api Extends common {

	/**
	 * function auc
	 * Connect to auc
	 *
	 * @param $url
	 * @param $array
	 * @return response string
	 */
	function auc($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_POST, TRUE);
		//curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($array));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_USERAGENT, "auc_api agent");
		//curl_setopt($ch, CURLOPT_COOKIEFILE, PLURK_COOKIE_PATH);
		//curl_setopt($ch, CURLOPT_COOKIEJAR, PLURK_COOKIE_PATH);
		$response = curl_exec($ch);
		$http_response = $response;
		$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		return ($response);
	}

	function get_watchlist($auc_id){
		$url=sprintf(AUC_WATCH_YQL_BASE,$auc_id,AUC_WATCH_YQL_TITLEIMG);
		$titleimg_obj=json_decode($this->auc(YQL.urlencode($url)));
		$url=sprintf(AUC_WATCH_YQL_BASE,$auc_id,AUC_WATCH_YQL_PRICE);
		$price_obj=json_decode($this->auc(YQL.urlencode($url)));

		$p_arr = $price_obj->query->results->p;
		$watchlist_arr=array();
		$i=0;
		foreach($titleimg_obj->query->results->a as $obj){
			$ret=array();
			$ret['href']=$obj->href;
			$ret['img']=$obj->img->src;
			$ret['title']=$obj->img->title;
			$ret['price']=$p_arr[$i];
			array_push($watchlist_arr,$ret);
			$i++;
		}
		return ($watchlist_arr);
	}

	function get_search_rss($q,$params){
		$url=sprintf(AUC_SEARCHRSS_YQL_BASE,$q,http_build_query($params));
		$search_obj=json_decode($this->auc(YQL.urlencode($url)));
		$search_arr=array();
		foreach($search_obj->query->results->item as $obj){
			$ret=array();
			$ret['href']=$obj->link;
			$ret['title']=$obj->title;
			$desc=$obj->description;
			if(preg_match('!src=\"(.+)\" height.+<b>(.+)</b>!',$desc,$matches))
			{
				$ret['img']=$matches[1];
				$ret['price']=$matches[2];
			}else{
				$ret['img']='';
				$ret['price']='';
			}
			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}

	function get_search_category($q,$params){
		$url=sprintf(AUC_SEARCH_YQL_BASE,$q,http_build_query($params),AUC_SEARCH_YQL_CATEGORY);
		$search_obj=json_decode($this->auc(YQL.urlencode($url)));
		$search_arr=array();
		foreach($search_obj->query->results->li as $obj){
			$ret=array();
			$ret['name'] = $obj->a->content;
			$ret['amount'] = str_replace(array('(',')'),'',$obj->span);
			if(preg_match('!&cat=(.+)!',$obj->a->href,$matches)){
				$ret['cat_id']=$matches[1];
			}else{
				$ret['cat_id']='';
			}

			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}
	
	function get_autocomplete($q){
		$url=sprintf(AUC_AUTOCOMPLETE,$q);
		$search_obj=json_decode($this->auc(YQL.urlencode($url)));
		$search_arr=array();
		foreach($search_obj->query->results->ResultSet->Result as $obj){
			$ret=array();
			$ret['title'] = $obj->Title;
			$ret['scale'] = $obj->Scale;

			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}
}
?>